package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private DishMapper dishMapper;

    @Override
    public boolean save(Category category) {


        //完善数据
        //创建时间
        category.setCreateTime(LocalDateTime.now());
        //修改时间
        category.setUpdateTime(LocalDateTime.now());

        //判断类别是否存在
        Category dbcategory = categoryMapper.findByName(category.getName());
        if (dbcategory!=null) {
            throw new BusinessException("类名已存在，请修改");
        }
        //否则添加
        return categoryMapper.save(category)>0;

    }

    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        //调用PageHelper工具类
        PageHelper.startPage(page,pageSize);
        List<Category> categoryList = categoryMapper.findAll();
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
       //新建new Page对象接受上面的 pageInfo的值
        Page<Category> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    @Override
    public boolean deleteById(Long id) {
        //查询当前类别才是否大于0  >0不能删除 提示用户不能删除
        Integer dishCount = dishMapper.countByCid(id);
        if (dishCount >0) {
            throw new BusinessException("该类别有菜品无法删除");
        }

        Integer setmealCount = setmealMapper.countByCid(id);
        if (setmealCount >0) {
            throw new BusinessException("该类别有套餐无法删除");
        }

        //如果上面都不成立 就执行删除
        return categoryMapper.deleteById(id)>0;
    }

    @Override
    public boolean update(Category category) {
        //判断类别是否重复修改的名字是否重复,如果已经存在,提示修改失败
        Category dbCategory = categoryMapper.findByName2(category);
        if (dbCategory!=null) {
            throw new BusinessException("已存在相同的类别名,修噶失败");
        }
        //完善修改时间
        category.setUpdateTime(LocalDateTime.now());
        //调用Mapper 进行修改

        return categoryMapper.update(category)>0;
    }


    //查询所有分类数据
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Override
    public List<Category> findAllByType(Integer type) {
        return categoryMapper.findAllByType(type);
    }


}
